<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
    <el-form-item label="工厂" prop="factory">
        <el-select
          v-model="queryParams.factory"
          placeholder="请选择"
          style="width: 240px"
          clearable filterable
          @change="handleSearchListChange('factory')"
        >
          <el-option
            v-for="(item, index) in factoryOptions"
            :key="index"
            :label="item.deptName"
            :value="item.deptCode"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工段名称" prop="workSection">
        <el-select
          v-model="queryParams.workSection"
          placeholder="请选择"
          clearable filterable
          style="width: 240px"
          @change="handleSearchListChange('workSection')"
        >
          <el-option
            v-for="(item, index) in workshopOptions"
            :key="index"
            :label="item.deptName"
            :value="item.deptCode"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产线名称" prop="productLine">
        <el-select
          v-model="queryParams.productLine"
          placeholder="请选择"
          style="width: 240px"
          clearable filterable
          @change="handleSearchListChange('productLine')"
        >
          <el-option
            v-for="(item, index) in lineOptions"
            :key="index"
            :label="item.deptName"
            :value="item.deptCode"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工艺段名称" prop="sectionCode">
        <el-select
          v-model="queryParams.sectionCode"
          placeholder="请选择"
          style="width: 240px"
          clearable filterable
          @change="handleSearchListChange('sectionCode')"
        >
          <el-option
            v-for="(item, index) in processOptions"
            :key="index"
            :label="item.deptName"
            :value="item.deptCode"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工单编号" prop="workOrderCode">
        <el-input
          v-model="queryParams.workOrderCode"
          placeholder="请输入工单编号"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="prodName">
        <el-input
          v-model="queryParams.prodName"
          placeholder="请输入产品名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ERP订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入ERP订单编号"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-select
          v-model="queryParams.source"
          placeholder="请选择来源"
          clearable filterable
          style="width: 240px"
        >
          <el-option
            v-for="(item, index) in order_source"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable filterable
          style="width: 240px"
        >
          <el-option
            v-for="(item, index) in work_order_status_show"
            :key="index"
            :label="item.label"
            :value="item.value"
            :disabled="item.value == 'TO_BEGIN' || item.value == 'IN_PRODUCTION'"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="date">
        <el-date-picker
          v-model="queryParams.date"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          style="width: 240px"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="typeList"
      max-height="550px"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="工单编号"
        prop="workOrderCode"
        :show-overflow-tooltip="true"
        width="140"
      >
        <template #default="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">{{
            scope.row.workOrderCode
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="产品编号" prop="prodCode" />
      <el-table-column label="产品名称" prop="prodName" />
      <el-table-column label="计划产量" prop="planNumber" />
      <el-table-column label="实际产量" prop="actualNumber" />
      <el-table-column label="单位" prop="unit" />
      <el-table-column label="计划开始时间" :width="120" prop="planStartTime">
        <template #default="scope">
          <span>{{ scope.row.planStartTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划结束时间" :width="120" prop="planEndTime">
        <template #default="scope">
          <span>{{ scope.row.planEndTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际开始时间" :width="120" prop="actualStartTime">
        <template #default="scope">
          <span>{{ scope.row.actualStartTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际结束时间" :width="120" prop="actualEndTime">
        <template #default="scope">
          <span>{{ scope.row.actualEndTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工厂" prop="factoryName" />
      <el-table-column label="工段" prop="workShopName" />
      <el-table-column label="产线" prop="lineName" />
      <el-table-column label="工艺段" prop="sectionName" />
      <el-table-column label="来源" prop="source"
        ><template #default="scope">
          <dict-tag :options="order_source" :value="scope.row.source" />
        </template>
      </el-table-column>
      <el-table-column label="ERP订单编号" prop="orderCode" />
      <el-table-column label="状态" prop="status"
        ><template #default="scope">
          <dict-tag :options="work_order_status" :value="scope.row.status" />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="workOrder">
import { historyList } from "@/api/base/workOrder/workOrder";
import { getFactoryTree, depeIdSelectList } from "@/api/system/dept";
import DetailData from "./data";

const { proxy } = getCurrentInstance();

const typeList = ref([]);
const { work_order_status } = proxy.useDict("work_order_status");
const work_order_status_show = computed(() => {
    const arr =[]
    work_order_status.value.forEach(element => {
      if(element.value == 'COMPLETED' || element.value == 'CANCELLED') {
        arr.push(element)
      }
    });
    return arr
})
const { order_source } = proxy.useDict("order_source");
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const dateRange = ref([]);
var factoryOptions = ref([]);
var workshopOptions = ref([]);
var lineOptions = ref([]);
var processOptions = ref([]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
  },
});

const { queryParams, form } = toRefs(data);
/** 查询设备类型列表 */
function getList() {
  loading.value = true;
  let newForm = JSON.parse(JSON.stringify(queryParams.value));
  if (queryParams.value?.date?.length) {
    newForm.planStartTime = queryParams.value.date[0];
    newForm.planEndTime = queryParams.value.date[1];
    delete newForm.date;
  }
  historyList(proxy.addDateRange(newForm)).then(
    (response) => {
      typeList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}

function getFactorys(query) {
  getFactoryTree(query).then((res) => {
    factoryOptions.value = res.data;
  });
}

function handleSearchListChange(type) {
  if (type == "factory") {
    depeIdSelectList({
      deptType: "workShop",
      deptId: queryParams.value.factory,
    }).then((res) => {
      workshopOptions.value = res.data;
      lineOptions.value = [];
      processOptions.value = [];
      queryParams.value.workSection = "";
      queryParams.value.productLine = "";
      queryParams.value.sectionCode = "";
    });
  }
  if (type == "workSection") {
    depeIdSelectList({
      deptType: "line",
      deptId: queryParams.value.workSection,
    }).then((res) => {
      lineOptions.value = res.data;
      processOptions.value = [];
      queryParams.value.productLine = "";
      queryParams.value.sectionCode = "";
    });
  }
  if (type == "productLine") {
    depeIdSelectList({
      deptType: "section",
      deptId: queryParams.value.productLine,
    }).then((res) => {
      processOptions.value = res.data;
      queryParams.value.sectionCode = "";
    });
  }
}

function handleDetail(row) {
  proxy.subPage(DetailData, { title: "历史工单详情", data: row });
}

/** 表单重置 */
function reset() {
  form.value = {
    classesCode: "",
    classesName: "",
    classesMode: "",
    planClassesScheduleList: [],
    remark: "",
  };
  proxy.resetForm("formRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.subPage(DetailData, { title: "新增工单", data: {} });
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  proxy.subPage(DetailData, { title: "编辑工单", data: row });
}

function handleConfirmPro(row) {
  const ids = row.id;
  proxy.$modal
    .confirm('确定开始生产"' + row.planName + '"吗？')
    .then(function () {
      return planEnable({ id: ids });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("确定开始生产成功");
    })
    .catch(() => {});
}

function handleCancel() {
  const ids = row.id;
  proxy.$modal
    .confirm('确定取消"' + row.planName + '"吗？')
    .then(function () {
      return planEnable({ id: ids });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("取消成功");
    })
    .catch(() => {});
}

function handleHas() {
  const ids = row.id;
  proxy.$modal
    .confirm('确定"' + row.planName + '"生产完成吗？')
    .then(function () {
      return planEnable({ id: ids });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("确定生产完成成功");
    })
    .catch(() => {});
}

getList();
getFactorys();
onMounted(async () => {
  if (proxy.$route.query.parentId) {
    handleAdd();
  }
});
</script>
